<template>
    <div class="menusetting">
        <el-card class="box-card">
            <el-input v-model="name" placeholder="请输入菜单名" class="w200"></el-input>
            <el-button type="primary" @click="initMen">初始化左侧菜单</el-button>
            <el-button type="primary" @click="searchAllMenu">查询所有菜单</el-button>
            <el-button type="primary" @click="addMenu">新增菜单</el-button>
            <el-tree :data="data" show-checkbox node-key="code" ref="tree" highlight-current :props="defaultProps"> </el-tree>
        </el-card>
        <!-- 添加菜单 -->
        <el-dialog title="新增菜单" :visible.sync="addDialogVisible" width="50%" @close="addDiaClosed">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="父级ID">
                    <el-cascader v-model="form.parentID" :options="data" @change="handleChange" :props="cateProps"></el-cascader>
                </el-form-item>
                <el-form-item label="是否公开">
                    <el-select v-model="form.isEnable" class="w220" placeholder="请选择">
                        <el-option v-for="item in chooseOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="权限">
                    <el-input v-model="form.name" class="w220"></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input v-model="form.name" class="w220"></el-input>
                </el-form-item>
                <el-form-item label="图标">
                    <el-input v-model="form.name" class="w220"></el-input>
                </el-form-item>
                <el-form-item label="路由">
                    <el-input v-model="form.name" class="w220"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="form.name" class="w220"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 修改菜单 -->
    </div>
</template>

<script>
import { queryMenu, initMenu, queryAllMenu } from '@/api/system.js'
export default {
  name: 'menusetting',
  data () {
    return {
      data: [],
      chooseOptions: [
        {
          label: '公开',
          value: true
        },
        {
          label: '不公开',
          value: false
        }
      ],
      caseCader: [],
      cateProps: {
        label: 'code',
        value: 'id',
        children: 'children',
        checkStrictly: true
      },
      isEnable: false,
      defaultProps: {
        children: 'children',
        label: 'code'
      },
      queryobj: {
        pagenum: 1,
        pagesize: 50
      },
      name: '',
      form: {},
      addDialogVisible: false
    }
  },
  created () {
    this.getMenuTree()
    this.searchAllMenu()
  },
  mounted () {

  },
  methods: {
    async getMenuTree () {
      const { Data } = await queryMenu({
        pageSize: this.queryobj.pagesize,
        pageIndex: this.queryobj.pagenum,
        name: this.name
      })
      console.log(Data)
    },

    // 初始化左侧菜单
    async initMen () {
      const confirmResult = await this.$confirm(
        '此操作将初始化左侧菜单, 是否继续?',
        '提示',
        {
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const data = await initMenu()
      console.log(data)
      if (data.code === 0) this.$message.success('初始化成功')
    //   if(data.)
    },

    // 查询所有菜单
    async searchAllMenu () {
      const { Data } = await queryAllMenu({
        pageSize: this.queryobj.pagesize,
        pageIndex: this.queryobj.pagesize
      })
      this.data = Data.TreeList
      console.log(Data)
    },

    async addDiaClosed () {

    },

    // 级联选择器选项
    handleChange (value) {
      console.log(value)
    },
    // 新增菜单
    async addMenu () {
      this.addDialogVisible = true
    }
  }
}
</script>

<style scoped>
.w200 {
    width: 200px;
    margin-bottom: 10px;
    margin-right: 10px;
}

.w220 {
    width: 220px;
}
</style>
